﻿// ===============================================================================
// XCI.WinForm.Platform 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using DevExpress.Utils.Design;
using DevExpress.XtraEditors.Registrator;
using DevExpress.XtraEditors.Repository;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using XCI.Core;
using XCI.Helper;

namespace XCI.Windows
{
    /// <summary>
    /// 数据字典下拉控件
    /// </summary>
    public class DicComboBoxEdit : ComboBoxEditx
    {
        static DicComboBoxEdit()
        {
            RepositoryItemDicComboBoxEdit.RegisterDicComboBoxEdit();
        }

        public DicComboBoxEdit()
        {
        }

        [DefaultValue("Name")]
        [SmartTagProperty("DisplayMember", "XCI", SmartTagActionType.RefreshBoundsAfterExecute)]
        public override string DisplayMember => "Name";

        [DefaultValue("Value")]
        [SmartTagProperty("ValueMember", "XCI", SmartTagActionType.RefreshBoundsAfterExecute)]
        public override string ValueMember => "Value";

        /// <summary>
        /// 是否显示全部项
        /// </summary>
        [DefaultValue(true)]
        [Category("XCI")]
        [Description("是否显示全部项")]
        public virtual bool AllItemVisible { get; set; } = true;

        /// <summary>
        /// 全部项文本
        /// </summary>
        [DefaultValue("全部分类")]
        [Category("XCI")]
        [Description("全部项文本")]
        public virtual string AllItemCaption { get; set; } = "全部";

        /// <summary>
        /// 字典编码
        /// </summary>
        [Category("XCI")]
        [Description("字典编码")]
        [DefaultValue(null)]
        public string DicCode { get; set; }

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (WinFormHelper.IsDesignMode()) return;
            if (string.IsNullOrEmpty(DicCode)) return;

            LoadDic(DicCode);
        }

        public void LoadDic(string code)
        {
            var items = App.Handler.GetDic(code).Items.ToList();
            if (AllItemVisible)
            {
                items.Insert(0, new DicItem
                {
                    Name = AllItemCaption,
                    Value = string.Empty
                });
            }
            DataSource = items;
        }

        [Category("XCI-Properties")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public new RepositoryItemDicComboBoxEdit Properties => base.Properties as RepositoryItemDicComboBoxEdit;

        public override string EditorTypeName => RepositoryItemDicComboBoxEdit.CustomEditName;
    }

    [UserRepositoryItem("RegisterDicComboBoxEdit")]
    public class RepositoryItemDicComboBoxEdit : RepositoryItemComboBoxEditx
    {
        static RepositoryItemDicComboBoxEdit()
        {
            RegisterDicComboBoxEdit();
        }

        public new const string CustomEditName = "DicComboBoxEdit";

        public RepositoryItemDicComboBoxEdit()
        {
        }

        public override string EditorTypeName => CustomEditName;

        public static void RegisterDicComboBoxEdit()
        {
            Image img = null;
            EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(CustomEditName,
                typeof(DicComboBoxEdit), typeof(RepositoryItemDicComboBoxEdit),
                typeof(ComboBoxEditxViewInfo), new ComboBoxEditxPainter(), true, img));
        }

        public override void Assign(RepositoryItem item)
        {
            BeginUpdate();
            try
            {
                base.Assign(item);
                if (!(item is RepositoryItemDicComboBoxEdit source)) return;
                //
            }
            finally
            {
                EndUpdate();
            }
        }
    }
}